//
//  ZFMenuView.swift
//  map
//
//  Created by fyzq on 2024/2/1.
//

import UIKit
import CollectionKit
import HandyJSON
import RxSwift
class ZFMenuView:ZFBaseView {
    private let collectionView = CollectionView()
    var subject = PublishSubject<ZFMenuModal>()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setZFMenuView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setZFMenuView(){
        
        let dataSource = ArrayDataSource(data: ZFMenuModal.menu())
        let provider = BasicProvider(
            dataSource: dataSource,
            viewSource: { (view: ZFMenuItemView, data: ZFMenuModal, index: Int) in
                view.modal = data
            },
            sizeSource: {(_,_,_ )-> CGSize in
                let width = UIDevice.width - 20;
                let item_width = (width - 6 * 10) / 5.0
                return CGSize(width: item_width, height: 70.0)
            },
            layout: FlowLayout(spacing: 10,alignItems: .start).inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)),
            tapHandler: {[weak self]context in
                guard let self = self else {return}
                self.subject.onNext(context.data)
//                print(context.index)
            }
        )
        collectionView.provider = provider;
        collectionView.isScrollEnabled = false
        addSubview(collectionView)
        
        var column =  dataSource.data.count / 5
        if dataSource.data.count % 5 != 0 {
            column += 1
        }
        
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo((20 + column * 70 + (column - 1) * 10 ))
        }

    }

    
}

class ZFMenuItemView :ZFBaseView {
    var titleLabel:UILabel!
    var imageView:UIImageView!
    var modal:ZFMenuModal!{
        didSet{
            titleLabel.text = modal.title
            imageView.kf.setImage(with: URL(string: modal.url!))
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setZFMenuItemView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    fileprivate func setZFMenuItemView(){
        
        imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.snp.makeConstraints { make in
            make.width.height.equalTo(45)
        }
        imageView.addRadius(radius: 5)
        
        titleLabel = UILabel()
        titleLabel.textColor = R.color.color_66()
        titleLabel.textAlignment = .center
        titleLabel.font = UIFont.systemFont(ofSize: 12)
        
        let stackView = UIStackView(arrangedSubviews: [imageView ,titleLabel])
        stackView.axis = .vertical
        stackView.spacing = 5
        stackView.alignment = .center
        stackView.distribution = .equalSpacing
        addSubview(stackView)
        
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
    }
}



class ZFMenuModal : HandyJSON {
    var title:String?
    var url:String?
    
    required init(){}
}


extension ZFMenuModal {
    
    static func menu()->[ZFMenuModal]{
        let arr = [["title":"叠加","url":"https://img0.baidu.com/it/u=3232582821,3516640051&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1706893200&t=db7b49775d49c48a2cd93ff017ec7d84"],
                   ["title":"工具","url":"https://img0.baidu.com/it/u=3232582821,3516640051&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1706893200&t=db7b49775d49c48a2cd93ff017ec7d84"],
                   ["title":"导航","url":"https://img0.baidu.com/it/u=3232582821,3516640051&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1706893200&t=db7b49775d49c48a2cd93ff017ec7d84"],["title":"历史馆","url":"https://img0.baidu.com/it/u=3232582821,3516640051&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1706893200&t=db7b49775d49c48a2cd93ff017ec7d84"],
                   ["title":"收藏","url":"https://img0.baidu.com/it/u=3232582821,3516640051&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1706893200&t=db7b49775d49c48a2cd93ff017ec7d84"],
                   ["title":"消息","url":"https://img0.baidu.com/it/u=3232582821,3516640051&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1706893200&t=db7b49775d49c48a2cd93ff017ec7d84"],
                   ["title":"行政区域","url":"https://img0.baidu.com/it/u=3232582821,3516640051&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1706893200&t=db7b49775d49c48a2cd93ff017ec7d84"],
                   ["title":"绘图","url":"https://img0.baidu.com/it/u=3232582821,3516640051&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1706893200&t=db7b49775d49c48a2cd93ff017ec7d84"],
                   ["title":"轨迹","url":"https://img0.baidu.com/it/u=3232582821,3516640051&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1706893200&t=db7b49775d49c48a2cd93ff017ec7d84"],
                   
        ]
        
        
        return [ZFMenuModal].deserialize(from: arr) as! [ZFMenuModal]
        
    }
}
